﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using OrmAuto;

public partial class Controls_GestioneAgentiControl : System.Web.UI.UserControl
{

		public int UserID { get; set; }
	 Querier mQuery = new Querier();
	 
		protected bool mAgenteOfferta ;

		protected void Page_Load(object sender, EventArgs e)
		{
				myHiddenUserID.Value = UserID.ToString();
				
				//myEntityDataSourceAgenti.Where =
				//"it.[FkUserID]==" + UserID.ToString() + " and it.[Attiva] == true";

				bindaDati();

				//myListViewGestione.DataKeyNames = new[] { "Id" };
				//myListViewGestione.DataSource = agentiOfferta;
				//myListViewGestione.DataBind();

		}//fM

		private void bindaDati()
		{
		  	myDetailsAgenti.DataKeyNames = new[] { "Id" };
		  	myGridViewAgenti.DataKeyNames = new[] { "Id" };
		  	
		  	
				// if( myRadioListAgenti.SelectedValue.Equals("AgenteOfferta"))
				//   mAgenteOfferta = true;
				// else
				//   mAgenteOfferta = false ;  
		  	
				//if(mAgenteOfferta)
				//{
				//  var agentiOfferta = mQuery.getOffertaMerci(true, UserID);
				//  myGridViewAgenti.DataSource = agentiOfferta;
				//}
				//else
				//{
						
				//}
				
				myGridViewAgenti.DataBind();

		}//fM

  

		protected void myGridViewAgenti_SelectedIndexChanged(object sender, EventArgs e)
		{
				GridViewRow row = ((GridView)sender).SelectedRow as GridViewRow;
				//var pathPage = ((GridView)sender).SelectedDataKey.Value.ToString() ;
				

				if (row.Controls.Count < 2) return;
				DataControlFieldCell cella = row.Controls[2] as DataControlFieldCell;			
				if(cella == null) return ;	
				string id = cella.Text;
				if (string.IsNullOrEmpty(id)) return;
				int IdAgente=0;
				if (!int.TryParse(id,out IdAgente)) return;
				
			 bindaDettagli(IdAgente);				
				
		}

		private void bindaDettagli(int pIdAgente)
		{

					 
		}//fM
		
		
		protected void myGridViewAgenti_Sorting(object sender, GridViewSortEventArgs e)
		{

		}
		protected void myGridViewAgenti_RowDeleting(object sender, GridViewDeleteEventArgs e)
		{
				bindaDati();
		}
		protected void myGridViewAgenti_PageIndexChanging(object sender, GridViewPageEventArgs e)
		{

		}
		protected void myDetailsAgenti_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
		{
				//Iterate though the values entered by the user and HTML encode 
				//the values. This helps prevent malicious values from being 
				//stored in the data source.
				Security sec = new Security(); 
				string valori=string.Empty ;
				string car= string.Empty ;
				
				for (int i = 0; i < e.NewValues.Count; i++)
				{
						if (e.NewValues[i] != null)
						{
						  valori =e.NewValues[i].ToString();
						  if( sec.CiSonoCarNonAmmessi(valori,out car)  )
						  {
										e.Cancel =true ;
										this.myLiteralMsg.Text = "<b>Errore, caratteri non ammessi :"+  car +" </b>"; 
						  }
								e.NewValues[i] = Server.HtmlEncode(valori);
						}
				}
    		
		}//fM
		
		protected void myDetailsAgenti_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
		{
				bindaDati();
		}
		protected void myDetailsAgenti_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
		{

		}
		protected void myDetailsAgenti_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
		{

		}

		protected void myDetailsAgenti_ModeChanging(object sender, DetailsViewModeEventArgs e)
		{

		}
		
		protected void myRadioListAgenti_SelectedIndexChanged(object sender, EventArgs e)
		{
				bindaDati();
		}

}//fC
